﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Model;
using DAL;
using Newtonsoft.Json;

namespace BLL
{
    public class LoginManager
    {

        private static EFHelper<OASystemEntities> _efHelper = new EFHelper<OASystemEntities>();

        #region 登录方法
        /// <summary>
        /// 登录方法
        /// </summary>
        /// <param name="account"></param>
        /// <returns></returns>
        public ResultMessage IsLogin(OA_UserAccount account)
        {
            try
            {
                var info = _efHelper.GetInfo<OA_UserAccount>(a => a.Account == account.Account && a.PassWord == account.PassWord);
                if (info == null)
                    return new ResultMessage() { Success = false, Message = "未查询到该账号存在" };

                //查询到该账号存在之后要判断该账号是否启用
                if (!info.IsEnable)
                    return new ResultMessage() { Success = false, Message = "该账号已禁用" };

                //将该员工信息保存到session里面
                System.Web.HttpContext.Current.Session["info"] = info;

                return new ResultMessage() { Success = true };


            }
            catch (Exception ex)
            {

                return new ResultMessage() { Success = false, Message = ex.Message };
            }
        } 
        #endregion


        #region 根据员工ID获取该员工基本信息
        /// <summary>
        /// 根据员工ID获取该员工基本信息
        /// </summary>
        /// <param name="userId"></param>
        /// <returns></returns>
        public static OA_UserBasic GetUserBasic(int userId)
        {
            try
            {
                if (userId == 0)
                    throw new Exception();
                var userBasic = _efHelper.GetInfo<OA_UserBasic>(b => b.UserId == userId);
                if (userBasic == null)
                    throw new Exception();
                return userBasic;
            }
            catch (Exception ex)
            {
                return new OA_UserBasic();
            }
        }
        
        #endregion


        #region 获取手机验证码

        /// <summary>
        /// 获取手机验证码
        /// </summary>
        /// <param name="phone"></param>
        /// <returns></returns>
        public object GetCode(string phone)
        {
            try
            {

                return MSM.AliyunHelper.SendMsg(phone, JsonConvert.SerializeObject(new { code = Code() }));
            }
            catch (Exception ex)
            {

                throw;
            }

        }


        public string Code()
        {
            var num = "";
            for (int i = 0; i < 4; i++)
            {
                Random rand = new Random();
                var a = rand.Next(1, 9);
                num += a;
            }

            System.Web.HttpContext.Current.Session["phone_Number"] = num;
            return num;

        } 
        #endregion

    }
}
